// @formatter:off
/**
 * @file default.js
 * @author Alejandro Dario Simi
 * @date $Date: 2013-06-17 01:26:11 +0000 (Mon, 17 Jun 2013) $
 *
 * $Id: default.js 74 2013-06-17 01:26:11Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/tags/wcomix-1.0-BETA1/themes/default/default.js $
 */
// @formatter:on

function CloseMenues(event) {
	for( i = 0; i < Menus.length; i++) {
		$(Menus[i]).css({
			visibility: "hidden"
		});
	}
}

function MenuPosition(caller, menu) {
	var x = caller.position().left;
	var y = caller.position().top + caller.height();

	if( (x + menu.width()) > $(document).width()) {
		x = $(document).width() - menu.width() - 2;
	}

	menu.css({
		position: "absolute",
		top: y + "px",
		left: x + "px"
	});
}

function ToogleMenu(caller, menu) {
	var visible = menu.css("visibility") == "visible";

	CloseMenues(null);

	if(visible) {
		menu.css({
			visibility: "hidden"
		});
	} else {
		menu.css({
			visibility: "visible"
		});
		MenuPosition(caller, menu);
	}
}

function bytesToSize(bytes) {
	var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
	if(bytes == 0) {
		return 'n/a';
	}
	var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
	return Math.round(bytes / Math.pow(1024, i), 2) + ' ' + sizes[i];
}

function ReloadHome() {
	window.location.href = '?group=' + GROUP_ID;
}

function SelfReload() {
	window.location.reload();
}

function SetMobile(service, status) {
	jQuery.post('service.php?action=' + service + '&mobile=' + ( status ? 'on' : 'off'), function(data) {
		var xml = $($.parseXML(data));
		if(xml.find("wc-success").length) {
			ReloadHome();
		} else if(xml.find("wc-failed").length) {
			var error = "";
			if(xml.find("wc-error>wc-code").length) {
				error = "[" + xml.find("wc-error>wc-code").text() + "] ";
			}
			error = error + xml.find("wc-error>wc-message").text();

			alert("Failed: " + error);
		}
	});
}

function LogOut(service, user) {
	jQuery.post('service.php?action=' + service + '&user=' + user, function(data) {
		var xml = $($.parseXML(data));
		if(xml.find("wc-success").length) {
			ReloadHome();
		} else if(xml.find("wc-failed").length) {
			var error = "";
			if(xml.find("wc-error>wc-code").length) {
				error = "[" + xml.find("wc-error>wc-code").text() + "] ";
			}
			error = error + xml.find("wc-error>wc-message").text();

			alert("Failed: " + error);
		}
	});
}

function TagButtons() {
	$(".TagButton").each(function() {
		var name = $(this).attr("TagButtonName");
		var style = $(this).attr("class");

		if(name && !/(.*)(TagButtonSPECIAL)(.*)/.test(style)) {
			for(var pattern in TagButtonPatterns) {
				if(TagButtonPatterns[pattern].test(name)) {
					style = style + " TagButton" + pattern;
					break;
				}
			}
		}

		$(this).attr("class", style);
	});
}

function UpdateSize(id, size) {
	var zImg = $("#ZOOM_IMG_" + id);
	var tImg = $("#Z_IMG_" + id);

	var iHeight = zImg.height();
	var iWidth = zImg.width();
	var tHeight = size;
	var tWidth = size;

	if(iWidth > iHeight) {
		tWidth = size
		tHeight = Math.round(size * iHeight / iWidth);
	} else {
		tWidth = Math.round(size * iWidth / iHeight);
		tHeight = size;
	}

	tImg.css({
		height: tHeight,
		width: tWidth
	});
}

function RemoveCollection(service, id, name) {
	if(confirm("Do you want to permanently remove collection '" + name + "'?")) {
		jQuery.post('service.php?action=' + service + '&group=' + GROUP_ID + '&collection=' + id + '&remove', function(data) {
			var xml = $($.parseXML(data));
			if(xml.find("wc-success").length) {
				SelfReload();
			} else if(xml.find("wc-failed").length) {
				var error = "";
				if(xml.find("wc-error>wc-code").length) {
					error = "[" + xml.find("wc-error>wc-code").text() + "] ";
				}
				error = error + xml.find("wc-error>wc-message").text();

				alert("Failed: " + error);
			}
		});
	}
}

function RemoveRule(service, id, name) {
	if(confirm("Do you want to permanently remove rule '" + name + "'?")) {
		jQuery.post('service.php?action=' + service + '&group=' + GROUP_ID + '&rule=' + id + '&remove', function(data) {
			var xml = $($.parseXML(data));
			if(xml.find("wc-success").length) {
				SelfReload();
			} else if(xml.find("wc-failed").length) {
				var error = "";
				if(xml.find("wc-error>wc-code").length) {
					error = "[" + xml.find("wc-error>wc-code").text() + "] ";
				}
				error = error + xml.find("wc-error>wc-message").text();

				alert("Failed: " + error);
			}
		});
	}
}

TipMessage = {
	'delay': 1500,

	'close': function() {
		TipMessage._close(this.level);
	},
	'open': function(template) {
		if(this.tip != null) {
			this.tip.remove();
			this.tip = null;
		}

		this.tip = jQuery('<div/>', {
			class: "TipMessageOPENED",
			html: template.html()
		});

		$("body").append(this.tip);

		this.tip.css({
			left: 10,
			bottom: this.tip.height() - 10
		});

		this.level++;
		setTimeout("TipMessage._close(" + this.level + ");", this.delay);
	},

	'tip': null,
	'level': 0,

	"_close": function(level) {
		if(this.tip != null && level == this.level) {
			this.tip.fadeOut(Math.round(this.delay / 2), function() {
				$(this).remove();
			});
			this.tip = null;
			this.level = 0;
		}
	}
}

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g, "");
};

//
// Setting main button handlers.
$(function() {
	$("#TopBarHome").bind("click", function() {
		ReloadHome();
		return false;
	});

	for( i = 0; i < Menus.length; i++) {
		$(Menus[i] + "Button").bind("click", function() {
			ToogleMenu($(this), $("#" + $(this).attr("id").replace("Button", "")));
			return false;
		});
	}

	$("body").bind("click", CloseMenues);
});

//
// Parsing buttons.
$(function() {
	$("a.SimpleButton").each(function() {
		var aux = "";

		aux = aux + '<div class="SimpleButton" id="' + $(this).attr("id") + '" onclick="window.location.href=\'' + $(this).attr("href") + '\';return false;">';
		aux = aux + $(this).html();
		aux = aux + '</div>';

		$(this).replaceWith(aux);
	});
	TagButtons();
});
